package com.xindz.hgq.database.greendao;

import org.greenrobot.greendao.converter.PropertyConverter;

import java.util.Arrays;
import java.util.List;

/**
 * @name HGQBase
 * @class name：com.xindz.hgq.database.greendao
 * @describe GreenDao 字段存储数组类型数据转换类 https://blog.csdn.net/zxm317122667/article/details/73528387
 * @anthor XINDZ
 * @time 2018/5/16 14:22
 * @change
 * @chang time
 * @class describe
 */
public class StringConverter implements PropertyConverter<List<String>, String> {
    @Override
    public List<String> convertToEntityProperty(String databaseValue) {
        if (null == databaseValue) {
            return null;
        } else {
            return Arrays.asList(databaseValue.split("&%&"));
        }

    }

    @Override
    public String convertToDatabaseValue(List<String> entityProperty) {
        if (entityProperty == null) {
            return null;
        } else {
            StringBuilder sb = new StringBuilder();
            for (String link : entityProperty) {
                sb.append(link);
                sb.append("&%&");
            }
            if (sb.length() > 3)
                sb.delete(sb.length() - 3, sb.length());
            return sb.toString();
        }
    }
}
